#include <QtTest>

#include <limits>

#include "../tests_common.h"

#include "bitwin/bit_range.h"


using namespace BitWin;


class TestBitRange: public QObject
{

    Q_OBJECT

private slots:

    void testConstruct_data();
    void testConstruct();
};


void TestBitRange::testConstruct_data()
{
    QTest::addColumn<BitRange>("range");
    QTest::addColumn<unsigned>("begin");
    QTest::addColumn<unsigned>("end");
    QTest::addColumn<unsigned>("length");
    
    unsigned upper_bound = std::numeric_limits<unsigned>::max();
    
    QTest::newRow("BitRange 0 to 4")
        << BitRange(0, 4) << 0U << 4U << 4U
    ;    
    QTest::newRow("BitRange 2 to 8")
        << BitRange(2, 8) << 2U << 8U << 6U
    ;
    QTest::newRow("BitRange 3 to 4")
        << BitRange(3, 4) << 3U << 4U << 1U
    ;
    QTest::newRow("BitRange 4 to 3")
        << BitRange(4, 3) << 3U << 5U << 2U
    ;
    QTest::newRow("BitRange 2 to 2")
        << BitRange(2, 2) << 2U << 3U << 1U
    ;
    QTest::newRow("BitRange 8 to 5")
        << BitRange(8, 5) << 5U << 9U << 4U
    ;
    QTest::newRow("Null range")
        << BitRange() << 0U << 0U << 0U
    ;
    QTest::newRow("Full range")
        << BitRange(0, upper_bound)
        << 0U << upper_bound << upper_bound
    ;
    QTest::newRow("BitRange 0 to 0")
        << BitRange(0, 0) << 0U << 1U << 1U
    ;
    QTest::newRow("BitRange MAX to MAX")
        << BitRange(upper_bound, upper_bound)
        << upper_bound << upper_bound << 1U
    ;
    QTest::newRow("Extremly big range(reversed)")
        << BitRange(upper_bound, 0)
        << 0U << upper_bound + 1 << upper_bound
    ;
}

void TestBitRange::testConstruct()
{
    QFETCH(BitRange, range);
    
    QFETCH(unsigned, begin);
    QCOMPARE(range.begin(), begin);
    
    QEXPECT_FAIL(
        "BitRange MAX to MAX",
        "There is no way to deal with such range",
        Continue
    );
    QFETCH(unsigned, end);
    QCOMPARE(range.end(), end);
    
    QEXPECT_FAIL(
        "Extremly big range(reversed)",
        "There is no way to deal with such range",
        Continue
    );
    QFETCH(unsigned, length);
    QCOMPARE(range.length(), length);
}


QTEST_MAIN(TestBitRange)


#include "main.moc"
